package com.sx.micowell.framework.model

import androidx.lifecycle.MutableLiveData
import com.example.mvvm.base.data.MutableLiveListData
import com.example.mvvm.base.vm.BaseViewModel
import com.example.mvvm.until.SPUtils
import com.google.gson.Gson
import com.miloyu.modelnet.network.ApiResponse
import com.miloyu.modelnet.network.HttpClient
import com.miloyu.modelnet.network.service.IApiService
import com.miloyu.modelnet.network.tools.RxUtils
import com.miloyu.modelnet.response.AppBanner
import com.miloyu.modelnet.response.ImagePrefixVO
import com.sx.micowell.framework.app.SPKey
import io.reactivex.Observable
import io.reactivex.Observer

/**
 * @Description
 * @Author 梁浩文
 * @Date 2021/9/23 9:53
 */
class SplashModel : BaseViewModel() {
    var mpUrl = MutableLiveData<AppBanner>()
    fun getUrl(cache: Boolean) {
        Observable.merge(
            HttpClient.instance.apiService.getImagePrefix(),
            HttpClient.instance.create(IApiService::class.java).getSplashUrl()
        )
            .compose(RxUtils.compose())
            .subscribe(object : ApiResponse<Any>() {
                override fun success(data: Any?, msg: String?) {
                    when(data) {
                        is ImagePrefixVO -> {
                            SPUtils.getInstance().put(SPKey.SP_IMAGE_PREFIX, data.prefix)
                        }
                        is AppBanner ->{
                            SPUtils.getInstance().put("urlJson", Gson().toJson(data))
                            if (!cache) {
                                mpUrl.postValue((data as AppBanner))
                            }
                        }
                    }

                }

                override fun onFail(e: Throwable?, errorMsg: String?) {

                }

            })
    }
}